package com.cande.jnr;

import jnr.ffi.*;
import jnr.ffi.Runtime;
import jnr.ffi.byref.PointerByReference;


public class JnrExample {
    public static void main(String[] args) {
        // 加载标准 C 库
        CLibrary clib = LibraryLoader.create(CLibrary.class).load("msvcrt");

        CrossDbLibrary crossDbLibrary = LibraryLoader.create(CrossDbLibrary.class).load("crossdb");
//        // 调用本地方法
//        clib.printf("Hello, World\n");


        Runtime runtime = Runtime.getSystemRuntime();
        Pointer phDb = Memory.allocate(runtime, 1);
        Pointer phTable = Memory.allocate(runtime, 200);
        Pointer phField = Memory.allocate(runtime, 1000);
        String dbName = "database_name";
//        #define	CROSS_ONDISK 			(0<<0)
//        #define	CROSS_RAMDISK			(1<<0)
//        #define	CROSS_INMEM 			(2<<0)
        int flags = 1;  // Example flags

        PointerByReference pointerByReference = new PointerByReference();

        int ret = crossDbLibrary.cross_dbCreate(phDb, dbName, flags);

        cross_field_t crossFieldT = new cross_field_t(runtime);
        crossFieldT.rsvd.set(1l);
        crossFieldT.useMemory(phField);

        int ret1 = crossDbLibrary.cross_dbTblCreate(phDb, phTable, "table", phField,"id" ,0);

        System.out.println(ret);
        System.out.println(ret1);
    }



    public static class cross_field_t extends Struct {
        public CrossFldEntry field_entry;
        public final Pointer pField = new Pointer();
        public final Struct.Unsigned32 rsvd = new Unsigned32();
        public final Pointer ref_obj = new Pointer();
        public final Pointer obj_name = new Pointer();
        public final Unsigned16 rsvd2 = new Unsigned16();

        public cross_field_t(Runtime runtime) {
            super(runtime);
            field_entry = new CrossFldEntry(runtime);
            field_entry.setFldName("id");
        }
    }


    public static class CrossFldEntry extends Struct{
        public java.lang.String fldName;
        public byte fldPriv;
        public byte fldType;
        public byte fldFormat;
        public int fldOffset;
        public int fldSize;
        public int fldFlags;
        public long fldCount;
        public short fldBitOffset;
        public short fldPriv1;
        public int fldPriv2;
        public int fldPriv3;
        public short fldPriv4;
        public short fldPriv5;

        public CrossFldEntry(Runtime runtime) {
            super(runtime);
        }

        public java.lang.String getFldName() {
            return fldName;
        }

        public void setFldName(java.lang.String fldName) {
            this.fldName = fldName;
        }

        public byte getFldPriv() {
            return fldPriv;
        }

        public void setFldPriv(byte fldPriv) {
            this.fldPriv = fldPriv;
        }

        public byte getFldType() {
            return fldType;
        }

        public void setFldType(byte fldType) {
            this.fldType = fldType;
        }

        public byte getFldFormat() {
            return fldFormat;
        }

        public void setFldFormat(byte fldFormat) {
            this.fldFormat = fldFormat;
        }

        public int getFldOffset() {
            return fldOffset;
        }

        public void setFldOffset(int fldOffset) {
            this.fldOffset = fldOffset;
        }

        public int getFldSize() {
            return fldSize;
        }

        public void setFldSize(int fldSize) {
            this.fldSize = fldSize;
        }

        public int getFldFlags() {
            return fldFlags;
        }

        public void setFldFlags(int fldFlags) {
            this.fldFlags = fldFlags;
        }

        public long getFldCount() {
            return fldCount;
        }

        public void setFldCount(long fldCount) {
            this.fldCount = fldCount;
        }

        public short getFldBitOffset() {
            return fldBitOffset;
        }

        public void setFldBitOffset(short fldBitOffset) {
            this.fldBitOffset = fldBitOffset;
        }

        public short getFldPriv1() {
            return fldPriv1;
        }

        public void setFldPriv1(short fldPriv1) {
            this.fldPriv1 = fldPriv1;
        }

        public int getFldPriv2() {
            return fldPriv2;
        }

        public void setFldPriv2(int fldPriv2) {
            this.fldPriv2 = fldPriv2;
        }

        public int getFldPriv3() {
            return fldPriv3;
        }

        public void setFldPriv3(int fldPriv3) {
            this.fldPriv3 = fldPriv3;
        }

        public short getFldPriv4() {
            return fldPriv4;
        }

        public void setFldPriv4(short fldPriv4) {
            this.fldPriv4 = fldPriv4;
        }

        public short getFldPriv5() {
            return fldPriv5;
        }

        public void setFldPriv5(short fldPriv5) {
            this.fldPriv5 = fldPriv5;
        }


    }

}
